package com.dcms.admin.controller;

import com.dcms.admin.bean.Template;
import com.dcms.admin.service.ITemplateService;
import com.dcms.util.ResultBean;
import com.dcms.util.ZIPUtil;
import org.apache.tools.ant.taskdefs.Ant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.io.File;
/**
 * Created by 90844 on 2018/2/25.
 */
@Controller
@RequestMapping("admin/template")
public class TemplateController {

    @Autowired
    private ITemplateService iTemplateService;

    /**
     * 转发到一个测试页面
     * @return
     */
    @RequestMapping("test")
    public String test(){
        return "admin/template/test";
    }

    /**
     * 上传模板
     * @param multipartFile
     * @param template
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("/uploadTemplate")
    public ResultBean uploadTemplate(MultipartFile multipartFile, Template template, HttpSession session)throws Exception{
        //1.获取文件原本的名字，判断是不是.zip后缀
         String originalFileName = multipartFile.getOriginalFilename();
         String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
         if (!".zip".equalsIgnoreCase(suffix)){
             return new ResultBean(10099,"上传格式有误，请选择zip文件！");
         }
         //2.随机给一个新的文件名,并准备一个路径：E:/upload/zip/用户帐号/日期/文件名
        String newFileName = UUID.randomUUID().toString()+suffix;
        String path = "E:/upload/zip/"+"steven"+"/"+ new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"/"+newFileName;
        //3.准备上传路径
        File uploadFile = new File(path);
        if(!uploadFile.exists()){
            uploadFile.mkdirs();
        }
        //4.向磁盘写文件
        try {
            multipartFile.transferTo(uploadFile);
        }catch (Exception e){
            return new ResultBean(10097,"上传失败");
        }

        //5.准备插入数据库的数据
        template.setTemplate_url(path);
        template.setTemplate_user_id(1);
         return iTemplateService.addTemplate(template);
    }

    /**
     * 解压模板
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/releaseTemplate")
    public ResultBean releaseTemplate(HttpServletRequest request)throws Exception{
        String pastePath = "E:/WorkSpace/vso/dcms/src/main/webapp/jsp/admin/template/zipfile";
        String path = "E:/upload/zip/steven/2018-02-25/hello.zip";
        ResultBean rs = ZIPUtil.releaseZip(path,pastePath);
        return rs;
    }

    /**
     *展示模板
     * 参数示例：【展示某用户上传的名字带有"红色"的模板】-->{bo_showMyTemplate:1,template_name:"红色"}  【展示所有模板】-->{}
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/showTemplates")
    public ResultBean showTemplates(HttpServletRequest request, HttpSession session,@RequestParam Map map)throws Exception{
        //1、将目前session里的登陆用户的id放入进参数
        map.put("template_user_id",null);
        //2、进行查询
        ResultBean rs = iTemplateService.showTemplates(map);
        return rs;
    }

    /**
     * 删除模板
     * 参数示例：【删除某用户所有模板】-->{template_user_id:1}   【删除某个模板】-->{template_id:1}
     * @return
     * @throws Exception
     */
    @RequestMapping("/removeTemplate")
    public ResultBean removeTemplate(@RequestParam Map map)throws Exception{
        return iTemplateService.removeTemplate(map);
    }
}
